package com.urms.controller.login;

import com.urms.entity.User;
import com.urms.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserSerivce userSerivce;

    /**
     * 用于接收用户登录请求
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/userLogin")
    public int doLogin(User user, HttpServletRequest request) {
        User u = userSerivce.selectByLoginNameAndPwd(user);
        HttpSession session = request.getSession(true);
        session.setMaxInactiveInterval(3600);
        if (Objects.nonNull(u)) {
            session.setAttribute("user",u);
            return 1;
        } else {
            return 0;
        }
    }
    @RequestMapping("/userLogout")
    public int userLogout(HttpServletRequest request){
        HttpSession session= request.getSession();
        session.removeAttribute("user");
        User user= (User) session.getAttribute("user");
        if (user==null){
            return 1;
        }else {
            return 0;
        }
    }
}
